<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\PayService;
use Illuminate\Http\Request;

class PayYueController extends BaseController
{
    //余额支付

    public function __construct(PayService $service)
    {
        $this->db = $service;
    }

    /**
     * 统一下单
     * type业务类型：
            1患者咨询医生开启对话uds_patient，
            2患者查看医生帖子tieba_pay，
            3患者发布代办预约订单gc_daiban，
            4患者发布小区医护预约订单gc_yihu，
            5患者发布陪诊预约订单gc_peizhen，
            6药品订单drug_order，
     */
    public function pay(Request $request)
    {
        $token = $request->post('token', '');
        $yw_type = $request->post('yw_type', 0);
        $yw_id = $request->post('yw_id', 0);
        $uOne = $this->db->userByToken($token);
        if (!in_array($yw_type, [1, 2, 3, 4, 5, 6])) {
            return $this->toJson(-2, '类型错误');
        }
        if (empty($uOne)) {
            return $this->toJson(-2, '没有登录');
        }
        if ($uOne['balance'] == 0) {
            return $this->toJson(-2, '没有余额');
        }
        $res = $this->db->yuPay($yw_type, $yw_id, $uOne);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}